//
//  come_back_demo.hpp
//  cpp_algorithm
//
//  Created by 李贺 on 2022/2/11.
//

#ifndef come_back_demo_hpp
#define come_back_demo_hpp

#include <stdio.h>
#include <vector>
#include <iostream>
#include <string>
#include <unordered_map>
#include <stack>
#include <queue>

using namespace std;

namespace fen_ge_hui_wen_chuan {
class Solution {
private:
    vector<vector<string>> result;
    vector<string> path; // 放已经回文的子串
    void backtracking (const string& s, int startIndex) {
        // 如果起始位置已经大于s的大小，说明已经找到了一组分割方案了
        if (startIndex >= s.size()) {
            result.push_back(path);
            return;
        }
        for (int i = startIndex; i < s.size(); i++) {
            if (isPalindrome(s, startIndex, i)) {   // 是回文子串
                // 获取[startIndex,i]在s中的子串
                string str = s.substr(startIndex, i - startIndex + 1);
                path.push_back(str);
            } else {                                // 不是回文，跳过
                continue;
            }
            backtracking(s, i + 1); // 寻找i+1为起始位置的子串
            path.pop_back(); // 回溯过程，弹出本次已经填在的子串
        }
    }
    bool isPalindrome(const string& s, int start, int end) {
        for (int i = start, j = end; i < j; i++, j--) {
            if (s[i] != s[j]) {
                return false;
            }
        }
        return true;
    }
public:
    vector<vector<string>> partition(string s) {
        result.clear();
        path.clear();
        backtracking(s, 0);
        return result;
    }
};
}

namespace pai_lie_wen_ti {
class Solution {
public:
    vector<vector<int>> result;
    vector<int> path;
    void backtracking (vector<int>& nums, vector<bool>& used) {
        // 此时说明找到了一组
        if (path.size() == nums.size()) {
            result.push_back(path);
            return;
        }
        for (int i = 0; i < nums.size(); i++) {
            if (used[i] == true) continue; // path里已经收录的元素，直接跳过
            used[i] = true;
            path.push_back(nums[i]);
            backtracking(nums, used);
            path.pop_back();
            used[i] = false;
        }
    }
    vector<vector<int>> permute(vector<int>& nums) {
        result.clear();
        path.clear();
        vector<bool> used(nums.size(), false);
        backtracking(nums, used);
        return result;
    }
};

class Solution1 {
    
public:
    vector<vector<int>> result;
    vector<int> path;
    
    void backtracking(vector<int> origins, vector<int> nums) {
        if (path.size() == origins.size()) {
            result.push_back(path);
            for (int i = 0; i < path.size(); i++) {
                cout << path[i] << "--";
            }
            cout << endl;
            return;
        }
        
        for (int i = 0; i < nums.size(); i++) {
            int value = nums[i];
            // 从 nums 数组中去掉 value
            vector<int> nums1 = nums;
            nums1.erase(nums1.begin() + i);
            path.push_back(value);
            backtracking(origins, nums1);
            path.pop_back();
        }
        
    }
    
    void test() {
        result.clear();
        path.clear();
        vector<int> origin = {1,2,3,4};
        backtracking(origin, origin);
    }
    
};

}

#endif /* come_back_demo_hpp */
